package com.tanhua.dubbo.api;

import com.mongodb.client.result.DeleteResult;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentsServiceApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public List<Comment> comments(Integer page, Integer pageSize, String movementId, Integer commentType) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(commentType);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created"))).skip(pageSize * (page - 1)).limit(pageSize);
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        return list;
    }

    @Override  //保存互动数据
    public Integer saveComment(Comment comment) {
        //获取操作类型，，以实现该方法的通用性,根据类型不同，改变更新字段，和返回总数的字段
        Integer commentType = comment.getCommentType();
        //保存互动数据
           //查询动态对象   查询动态发布人id存到comment
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        comment.setPublishUserId(movement.getUserId());
        mongoTemplate.save(comment);
        //更新评论二合一，更新的同时返回最新数据
        Criteria criteria = Criteria.where("id").is(comment.getPublishId());
        Query query = Query.query(criteria);
        Update update = new Update();
        if(commentType==1){
            update.inc("likeCount",1);
        }
        else if(commentType==2){
            update.inc("commentCount",1);//inc可让某个字段的值增长
        }else {
            update.inc("loveCount",1);
        }

        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(true);  //设置更新成功后获取最新数据对象.默认false
        //查询条件  更新字段的内容 配置 实体类
        Movement andModify = mongoTemplate.findAndModify(query, update, findAndModifyOptions, Movement.class);
        if(commentType==1){
            return andModify.getLikeCount();
        }
        else if(commentType==2){
            return andModify.getCommentCount();
        }else {
            return andModify.getLoveCount();
        }

    }

    @Override  //删除互动数据，返回互动总数
    public Integer delete(Integer commentType, Long userId, String id) {
        //1.删除互动数据  delete from tab where userId = ? and commentType = ? and movementId=?   动态id字段类型要注意
        Criteria criteria = Criteria.where("userId").is(userId).and("commentType").is(commentType).and("publishId").is(new ObjectId(id));
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);
        //2.更新动态表中的总数字段，同时获取最新值  更新就要有update对象
        Criteria criteria1 = Criteria.where("id").is(new ObjectId(id));
        Query query1 = Query.query(criteria1);
        Update update = new Update();
        if(commentType==1) {
            update.inc("likeCount", -1);
        }else if(commentType==2){
            update.inc("commentCount", -1);
        }else {
            update.inc("loveCount",-1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);  //设置更新同时返回最新数据
        Movement andModify = mongoTemplate.findAndModify(query1, update, options, Movement.class);
        if(commentType==1){
            return andModify.getLikeCount();
        }else if (commentType==2){
            return andModify.getCommentCount();
        }else {
            return andModify.getLoveCount();
        }

    }
    //更新动态中的评论总数
//    Criteria criteria = Criteria.where("id").is(comment.getPublishId());
//    Query query = Query.query(criteria);
//    Update update = new Update();
//        update.inc("commentCount",1);//inc可让某个字段的值增长
//        mongoTemplate.updateFirst(query,update,Movement.class); //查询条件，更新字段内容，实体类
//    //返回互动总数
//    Movement movement1 = mongoTemplate.findById(comment.getPublishId(), Movement.class);
//    Integer commentCount = movement1.getCommentCount();
//        return commentCount;
}
